﻿using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.ServiceModel.DomainServices.Client;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using EnsureWeb.Web.DataModel;
using EnsureWeb.UI.Core;
using System.Reflection;
using System;
using EnsureWeb.UI.Controls;

namespace EnsureWeb.UI.ViewModels
{
    [StaticResource("MainViewModel")]
    public partial class MainViewModel : MyViewModelBase
    {
        private object  rightTitle;
        public object RightTitle
        {
            get
            {
                return rightTitle;
            }
            set
            {
                rightTitle = value;
                RaisePropertyChanged("RightTitle");
            }
        }
        
        public ICommand adminContainerLoaded;
        public ICommand AdminContainerLoaded
        {
            get
            {
                if (adminContainerLoaded == null)
                    adminContainerLoaded = new RelayCommand<Panel>(e =>
                    {
                        this.adminContainer = e;
                        //show MembershipUserViewModel by defalut
                        this.ShowView("MembershipUserViewModel");
                        this.RightTitle = "User management";
                    });

                return adminContainerLoaded;
            }
        }
        
        public ICommand adminMenuItemClick;
        public ICommand AdminMenuItemClick
        {
            get
            {
                if (adminMenuItemClick == null)
                    adminMenuItemClick = new RelayCommand<AdminMenuItem>(adminMenuItem =>
                    {
                        this.ShowView(adminMenuItem.NavigateView);

                        this.RightTitle = adminMenuItem.Content;
                    });

                return adminMenuItemClick;
            }
        }

        private void ShowView(string viewName)
        {
            if (string.IsNullOrEmpty(viewName))
                return;


            if (viewName == "None")
            {
                EnsureWeb.Controls.Window.Alert("This function hasn't implemented");
                return;
            }

            var dummy = AdminContainerLoaded;


            UserControl view = this.adminContainer.FindName(string.Format("AdminContainer_{0}", viewName)) as UserControl;


            if (view == null)
            {
                view = this.CreateView(viewName);
                if (view != null)
                {
                    view.Name = string.Format("AdminContainer_{0}", viewName);
                    this.adminContainer.Children.Add(view);
                }
            }
            else
            {
                this.adminContainer.Children.Remove(view);
                this.adminContainer.Children.Add(view);
            }
        }

        private UserControl CreateView(string viewName)
        {
            if (viewName != string.Empty)
            {
                Assembly executingAssembly = Assembly.GetExecutingAssembly();
                foreach (Type type in executingAssembly.GetTypes())
                {
                    if (type.Name.EndsWith(viewName))
                    {
                        UserControl panel = Activator.CreateInstance(type) as UserControl;
                        return panel;
                    }
                }
            }

            return null;
        }

        private Panel adminContainer;
    }

}